package com.zq.thread.volatile1;

import java.util.concurrent.TimeUnit;

/**
 * @Author: zhaoqi
 * @Date: 11/23/2019 11:03 AM
 * @Version 1.0
 */
//volatile 线程之间的通信，通信的哪个变量需要被volatile修饰

public class Volitile implements Runnable {

    private static volatile boolean flag = true;

    @Override
    public void run() {
        while (flag){
            System.out.println(Thread.currentThread().getName() + "正在运行!");
        }
        System.out.println(Thread.currentThread().getName() + "运行结束!");
    }


    public static void main(String[] args) throws InterruptedException {
        Volitile avolitile = new Volitile();
        new Thread(avolitile,"thread A").start();
        System.out.println("主线程正在运行！");
        TimeUnit.MILLISECONDS.sleep(10);
        avolitile.stopThread();
    }

    private void stopThread() {
        flag = false;
    }
}

